package com.qm.utils;

import com.qm.finger.protocol.FingerProtocol;

import java.io.IOException;
import java.io.InputStream;
import java.util.Timer;
import java.util.TimerTask;

/**
 * create by steven
 * 2021-06-17
 * <p>
 * des:
 */
public class ReadCollectUtils {
    private static Timer mTimer;//定时器读取指纹问题
    private static ReadCollectUtils readCollectUtils;
    private int ret = 0;
    private FingerListen mFingerListen;
    private InputStream mInputStream;
    private boolean isStop = false;

    public static ReadCollectUtils getInstance() {
        if (mTimer == null)
            mTimer = new Timer();
        if (readCollectUtils == null) {
            synchronized (ReadCollectUtils.class) {
                readCollectUtils = new ReadCollectUtils();
            }
        }
        return readCollectUtils;
    }

    public void closeFinger() {
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }
    }

    //开始注册和比对监听串口数据
    public void startCollectRead(FingerListen fingerListen, InputStream inputStream) {
        int total = 207;
        this.mFingerListen = fingerListen;
        this.mInputStream = inputStream;
        if (mTimer != null) {
            mTimer.schedule(new TimerTask() {
                @Override
                public void run() {
                    if (!isStop) {
                        try {
                            if (mInputStream == null) {
                                mFingerListen.onFail();
                                mTimer.cancel();
                                mTimer = null;
                                return;
                            }
                            byte[] last;
                            int readLen = mInputStream.available();
                            byte[] readData = new byte[readLen];
                            if (readLen > 0 && readLen >= mInputStream.available()) {
                                ret = mInputStream.read(readData, 0, readData.length);
                                last = readData;
                                int readTimes = 10;
                                while (readTimes-- > 0) {
                                    try {
                                        readLen = mInputStream.available();
                                        Thread.sleep(40);
                                        if (readLen > 0 && readLen >= mInputStream.available()) {
                                            byte[] bt = new byte[readLen];
                                            ret = mInputStream.read(bt, 0, bt.length);
                                            byte[] center = new byte[last.length + readLen];
                                            System.arraycopy(bt, 0, center, last.length, bt.length);
                                            last = center;
                                            if (last.length == 207)
                                                break;
                                        }
                                    } catch (IOException | InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }
                                System.arraycopy(readData, 0, last, 0, readData.length);
                                //解析一下数据
                                mFingerListen.onSuccess(FingerProtocol.ParseReadFeatureProtocol(last));
                            }

                        } catch (IOException e) {
                            e.printStackTrace();
                            mFingerListen.onFail();
                            mTimer.cancel();
                            mTimer = null;
                        }
                    }
                }
            }, 20, 20);

        }
    }
}
